package cn.bytecollege.user.controller;

import cn.bytecollege.common.constant.Resource;
import cn.bytecollege.common.entity.Cart;
import cn.bytecollege.user.service.CartService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "UserCartDeleteController", value = "/user/UserCartDelete")
public class UserCartDeleteController extends HttpServlet {
    private CartService cartService = null;
    public UserCartDeleteController() {
        cartService = new CartService();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cuisineId = request.getParameter("cuisineId");
        Integer tempCartId = null;
        if (cuisineId != null){
             tempCartId = cartService.findCartIdByCusineId(Integer.valueOf(cuisineId));
        }
        String id = request.getParameter("cartId");
        Integer cartId = null;
        if (tempCartId == null){
             cartId =Integer.valueOf(id);
        }else {
             cartId =Integer.valueOf(tempCartId);
        }
        Integer i = cartService.findByCartId(cartId);
        Cart cart = new Cart();
//判断数量是否大于一
        if(i>1){
            cart.setNumber(i-1);
            cart.setCartId(cartId);
            cartService.updateByCartId(cart);
            response.sendRedirect(request.getContextPath()+"/user/userIndex");
            return;
        }
        cartService.deleteById(cartId);
       response.sendRedirect(request.getContextPath()+"/user/userIndex");

    }
}
